package com.example.bookdemo.Intercepor;

import com.example.bookdemo.util.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

//登录拦截器

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userToken = request.getHeader("user_token");
        log.info("从header中获取信息,token:{}",userToken);
        Claims claims = JWTUtil.verify(userToken);
        if (claims==null){
            response.setStatus(401);
            log.error("用户校验失败");
            return false;
        }
        return true;
    }
}
